﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Legazpi.Payroll.Services.Interfaces;
using System.Data.Entity;
using Emz.Core.Repositories;
using Emz.Core.Helpers;
using Emz.Core.Models;
using Emz.Core.Base;

namespace Legazpi.Payroll.Services.Services
{
    public class PhilHealthTableService : IPhilHealthTableService
    {

        #region Fields

        private DbContext _dbContext;
        private IPhilHealthTableRepository _philHealthRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public PhilHealthTableService()
            : this(new DatabaseFactory())
        {

        }

        public PhilHealthTableService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _philHealthRepository = new PhilHealthTableRepository(_dbContext);
            }
        }

        #endregion

        public PhilHealthTable GetById(int id)
        {
            return _philHealthRepository.GetById(id);
        }

        public IEnumerable<PhilHealthTable> GetAll()
        {
            return _philHealthRepository.GetAll();
        }

        public IEnumerable<PhilHealthTable> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(PhilHealthTable philHealth)
        {
            _philHealthRepository.Add(philHealth);
        }

        public void Update(PhilHealthTable philHealth)
        {
            _philHealthRepository.Update(philHealth);
        }

        public void Delete(int id)
        {
            var philHealth = GetById(id);
            if (philHealth != null)
                _philHealthRepository.Delete(philHealth);
        }

        public bool Save()
        {
            string message;
            _philHealthRepository.Save(out message);
            return message == "Saved";
        }
    }
}
